<?php
if(!defined('IN_WEB')) {
	exit('Access Denied');
}

class IndexController extends Controller{
	function __construct(){
		parent::__construct();
		$this->userAction = new UserAction();
		$this->admin_url = 'admin.php';
	}
	
	function index(){
		$this->smarty->display('admin/index.tpl');
	}
	
	function install(){
		//安装数据库
		$installAction = new InstallAction();
		$installAction->createTable();
		exit('install success');
	}
	
	function login(){
		if(empty($_POST['login_token'])){
			$this->userAction->logout();
		}
		
		if(isset($_POST['login_token'])){
			$result = $this->userAction->doLogin();
			switch ($result){
				case -1:
			    case -4:
					$msg = '不允许的登录';
					break;
				case -2:
				case -3:
					$msg = '用户名密码错误';
					break;
				case 1:
					header('Location:'.$this->admin_url);
					exit();
					break;
				default:
					$msg = '未知登录错误';
			}
		}
		
		$_SESSION['login_token'] = random(10);
		$this->smarty->assign('login_token', $_SESSION['login_token']);
		$this->smarty->assign('msg', $msg);
		$this->smarty->display('admin/login.tpl');
	}
	
	function server_info(){
		phpinfo();
	}
}